//
// Created by francklinson on 2021/10/21 AT 16:47.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>
#include <string>

using namespace std;

class Solution {
private:
    vector<vector<int>> movements{{-1, 0},
                                  {-1, 1},
                                  {0,  1},
                                  {1,  1},
                                  {1,  0},
                                  {1,  -1},
                                  {0,  -1},
                                  {-1, -1}};
    int m, n;
public:
    void gameOfLife(vector<vector<int>> &board) {
        // 根据板子当前的状态 更新到下一个状态
        // 先用额外的数组
        m = board.size();
        n = board[0].size();
        vector<vector<int>> ans(m, vector<int>(n, 0));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                int liveCellNum = calc(board, i, j); // 计算周围的活细胞个数
                // 原来是活细胞
                if (board[i][j]) {
                    if (liveCellNum < 2 or liveCellNum > 3)
                        ans[i][j] = 0;
                    else
                        ans[i][j] = 1;
                }
                    // 原来是死细胞
                else {
                    if (liveCellNum == 3)
                        // 死细胞复活
                        ans[i][j] = 1;
                }
            }
        }
        board = ans;
    }

    int calc(vector<vector<int>> &board, int i, int j) {
        // 计算board[i][j]周围8个格子里有多少个活细胞
        int ans = 0;
        int newI = i, newJ = j;
        for (int k = 0; k < 8; ++k) {
            newI = movements[k][0] + i;
            newJ = movements[k][1] + j;
            if (newI >= 0 and newI < m and newJ >= 0 and newJ < n) {
                ans += board[newI][newJ];
            }
        }
        return ans;
    }

};

class Solution2 {
public:
    void gameOfLife(vector<vector<int>> &board) {
        // 借用额外的状态变量进行存储
        int neighbors[3] = {0, 1, -1};

        int rows = board.size();
        int cols = board[0].size();

        // 遍历面板每一个格子里的细胞
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                // 对于每一个细胞统计其八个相邻位置里的活细胞数量
                int liveNeighbors = 0;
                for (int i : neighbors) {
                    for (int j : neighbors) {
                        if (!(i == 0 && j == 0)) {
                            // 相邻位置的坐标
                            int r = (row + i);
                            int c = (col + j);
                            // 查看相邻的细胞是否是活细胞
                            // 注意这里用了abs  包含了原来是活的 现在死了的细胞状态
                            if ((r < rows && r >= 0) && (c < cols && c >= 0) && (abs(board[r][c]) == 1)) {
                                liveNeighbors += 1;
                            }
                        }
                    }
                }
                // 规则 1 或规则 3
                if ((board[row][col] == 1) && (liveNeighbors < 2 || liveNeighbors > 3)) {
                    // -1 代表这个细胞过去是活的现在死了
                    board[row][col] = -1;
                }
                // 规则 4
                if (board[row][col] == 0 && liveNeighbors == 3) {
                    // 2 代表这个细胞过去是死的现在活了
                    board[row][col] = 2;
                }
            }
        }

        // 遍历 board 得到一次更新后的状态
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (board[row][col] > 0) {
                    board[row][col] = 1;
                } else {
                    board[row][col] = 0;
                }
            }
        }
    }
};

int main() {
    vector<vector<int>> board{{0, 1, 0},
                              {0, 0, 1},
                              {1, 1, 1},
                              {0, 0, 0}};
    Solution solution;
    solution.gameOfLife(board);
    for (int i = 0; i < board.size(); ++i) {
        for (int j = 0; j < board[0].size(); ++j) {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

